﻿#ifndef _DATABASE_VECTOR_SOURCE_FACTORY_H_H_
#define _DATABASE_VECTOR_SOURCE_FACTORY_H_H_

#include "udatabase/dll_cfg.h"
#include "graphicengine/OgreCommon.h"
#include "udatabase/interface/idatasourcefactory.h"
#include "udatabase/interface/ipropertyset.h"
#include "udatabase/geodb/databasevectorsource.h"
#include "kernel/stringutil.h"
#include "kernel/threading/mutex.h"

namespace FD
{
	namespace Utility
	{
		namespace DB
		{
			/// <summary>
			/// 矢量数据源工厂类
			/// </summary>
			class FD_DB_Export CDatabaseVectorSourceFactory :
				public FD::Utility::DB::Interface::IDataSourceFactory
			{
			public:		
				/// <summary>
				/// 默认析构函数
				/// </summary>
				virtual ~CDatabaseVectorSourceFactory();

				/// <summary>
				/// 返回单例的数据源工厂句柄
				/// </summary>
				/// <returns>失败则返回NULL</returns>
				static CDatabaseVectorSourceFactory* getSingletonPtr();

				/// <summary>
				/// 获取类型
				/// </summary>
				/// <param name=""></param>
				/// <returns>返回类型</returns>
				virtual int32 getType() const;
				/// <summary>
				/// 释放数据源
				/// </summary>
				/// <param name="pDataSource">数据源</param>
				/// <returns></returns>
				virtual void destroyDataSource(FD::Utility::DB::Interface::IDataSource* pDataSource);
				/// <summary>
				/// 从xml创建数据源
				/// </summary>
				/// <param name="xml">序列化德xml</param>
				/// <returns>返回数据源</returns>
				virtual FD::Utility::DB::Interface::IDataSource* createFromXML(const String &xml);
				/// <summary>
				/// 通过路径打开数据源
				/// </summary>
				/// <param name="path">路径</param>
				/// <param name="fileName">文件名</param>
				/// <returns>返回数据源</returns>
				virtual FD::Utility::DB::Interface::IDataSource * openDataSource(const String &path);
				/// <summary>
				/// 通过连接参数打开数据源
				/// </summary>
				/// <param name="connection">连接参数</param>
				/// <returns>返回数据源</returns>
				virtual FD::Utility::DB::Interface::IDataSource * openDataSource(FD::Utility::DB::Interface::IPropertySet * connection);
				/// <summary>
				/// 通过路径创建数据源
				/// </summary>
				/// <param name="path">路径</param>
				/// <param name="fileName">文件名</param>
				/// <returns>返回数据源</returns>
				virtual FD::Utility::DB::Interface::IDataSource * createDataSource(const String &path);
				/// <summary>
				/// 通过连接参数创建数据源
				/// </summary>
				/// <param name="connection">连接参数</param>
				/// <returns>返回数据源</returns>
				virtual FD::Utility::DB::Interface::IDataSource * createDataSource(FD::Utility::DB::Interface::IPropertySet * connection);

			private:
				//CDatabaseVectorSourceFactory(FD::GraphicEngine::NameValuePairList *pList);
			public:
				CDatabaseVectorSourceFactory();
			private:
				CDatabaseVectorSourceFactory(CDatabaseVectorSourceFactory & obj);
			private:
				static CDatabaseVectorSourceFactory* mSingletonPtr;
			};

			class FD_DB_Export CDatabaseVectorSourceManager
			{
			protected:
				CDatabaseVectorSourceManager();

				C_DISABLE_COPY(CDatabaseVectorSourceManager);

			protected:

				static CDatabaseVectorSourceManager* mpSingleton;
				static FD::Kernel::RecursiveMutex mInstanceMutex;

				struct SourceItem
				{
					CDatabaseVectorSource* pSource;
					UINT16 refCount;
				};

				typedef std::map<String, SourceItem>  VectorSourceMap;
				VectorSourceMap mSourceMap;
				FD::Kernel::RecursiveMutex mSourceMutex;
			public:
				static CDatabaseVectorSourceManager* getSingletonPtr();
				static void releaseSingleton();
			public:
				// dbType : 取值为 SQLITE / ORACLE (暂时支持这两种，后续会增加SQLSERVER)
				// dbName : 如果是SQLITE数据库，则dbName为SQLite文件路径(相对目录/绝对目录)，否则为数据库名称		
				// 如果是SQLITE数据库，其余带有默认值的参数可不传入
				CDatabaseVectorSource* openVectorSource(const String& dbType, const String& dbName, 
					const String& driverName =  FD::Kernel::StringUtil::BLANK,
					const String& hostName = FD::Kernel::StringUtil::BLANK, uint32 port = 0,
					const String& userName = FD::Kernel::StringUtil::BLANK, const String& password  = FD::Kernel::StringUtil::BLANK,
					const String& connectOption = FD::Kernel::StringUtil::BLANK);

				CDatabaseVectorSource* createVectorSource(const String& dbType, const String& dbName, 
					const String& driverName =  FD::Kernel::StringUtil::BLANK,
					const String& hostName = FD::Kernel::StringUtil::BLANK, uint32 port = 0,
					const String& userName = FD::Kernel::StringUtil::BLANK, const String& password  = FD::Kernel::StringUtil::BLANK,
					const String& connectOption = FD::Kernel::StringUtil::BLANK);

				void addVectorSourceRef(CDatabaseVectorSource* pVectorSource);
				void releaseDataSourceRef(CDatabaseVectorSource* pVectorSource);
			};
		}
	}
} // End of namespaces


#endif //_DATABASE_VECTOR_SOURCE_FACTORY_H_H_

